// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.warehouse.viewers.wizards;

import org.dengues.core.warehouse.ENodeCategoryName;
import org.dengues.core.warehouse.ENodeStatus;
import org.dengues.designer.ui.DesignerUIPlugin;
import org.dengues.model.database.DatabaseDiagram;
import org.dengues.warehouse.i18n.Messages;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-11-27 qiang.zhang $
 * 
 */
public abstract class NewElementWizardPage2 extends AbstractStorageWizardPage {

    protected IStatus nameStatus;

    protected DatabaseDiagram database;

    protected IPath path;

    protected Text nameText;

    protected String databaseName;

    private Button button;

    protected Composite container;

    protected ENodeCategoryName name;

    /**
     * Qiang.Zhang.Adolf@gmail.com NewDatabaseWizardPage constructor comment.
     * 
     * @param database
     * @param isActived
     */
    public NewElementWizardPage2(IPath path, DatabaseDiagram database, ENodeCategoryName name) {
        super("NewDatabaseWizardPage.2"); //$NON-NLS-1$
        this.database = database;
        this.path = path;
        nameStatus = createOkStatus();
        this.name = name;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
     */
    public void createControl(Composite parent) {
        GridLayout layout = new GridLayout();
        GridData gridData = new GridData(GridData.FILL_BOTH);
        container = new Composite(parent, SWT.NONE);
        layout = new GridLayout();
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        container.setLayout(layout);
        gridData = new GridData(GridData.FILL_BOTH);
        container.setLayoutData(gridData);
        Composite generic = new Composite(container, SWT.NONE);
        layout = new GridLayout(3, false);
        layout.marginHeight = 0;
        layout.marginWidth = 0;
        generic.setLayout(layout);
        gridData = new GridData(GridData.FILL_HORIZONTAL);
        generic.setLayoutData(gridData);
        Label nameLab = new Label(generic, SWT.NONE);
        nameLab.setText(name.getDisplayName());

        nameText = new Text(generic, SWT.BORDER);
        nameText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

        button = new Button(generic, SWT.PUSH);
        button.setText("...");//$NON-NLS-1$
        setControl(container);
        addListeners();
        checkFieldsValue();
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "checkFieldsValue".
     */
    protected void checkFieldsValue() {
        if (nameText.getText().length() == 0) {
            nameStatus = new Status(IStatus.ERROR, DesignerUIPlugin.PLUGIN_ID, IStatus.ERROR, Messages.getString(
                    "NewXmlFileWizardPage2.Empty", name.getDisplayName()), null); //$NON-NLS-1$
        } else {
            nameStatus = createOkStatus();
        }
        setMessage(nameStatus);
        boolean b = nameStatus.getSeverity() != IStatus.ERROR;
        setPageComplete(b);
        if (b) {
            databaseName = nameText.getText();
        }
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "addListeners".
     */
    protected void addListeners() {
        nameText.addModifyListener(new ModifyListener() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.ModifyListener#modifyText(org.eclipse.swt.events.ModifyEvent)
             */
            public void modifyText(ModifyEvent e) {
                checkFieldsValue();
            }
        });
        button.addSelectionListener(new SelectionAdapter() {

            /*
             * (non-Javadoc)
             * 
             * @see org.eclipse.swt.events.SelectionAdapter#widgetSelected(org.eclipse.swt.events.SelectionEvent)
             */
            @Override
            public void widgetSelected(SelectionEvent e) {
                FileDialog dialog = new FileDialog(getShell());
                String[] strings = new String[] { "*.xml", "*.csv", "*.xls" };
                switch (name) {
                case CSVFILE:
                    strings = new String[] { "*.csv" };
                    break;
                case XMLFILE:
                    strings = new String[] { "*.xml" };
                    break;
                case EXCELFILE:
                    strings = new String[] { "*.xls" };
                    break;
                default:
                    break;
                }
                dialog.setFilterExtensions(strings);
                String open = dialog.open();
                if (open != null) {
                    databaseName = open;
                    setFileName(open);
                }
            }
        });
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "setFileName".
     * 
     * @param open
     */
    protected abstract void setFileName(String open);

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "getDatabaseStatus".
     * 
     * @return
     */
    public String getDatabaseStatus() {
        return ENodeStatus.NORMAL.getId();
    }

    private void setMessage(IStatus status) {
        if (IStatus.ERROR == status.getSeverity()) {
            setErrorMessage(status.getMessage());
            setMessage(""); //$NON-NLS-1$
        } else {
            setMessage(status.getMessage());
            setErrorMessage(null);
        }
    }

    private static IStatus createOkStatus() {
        return new Status(IStatus.OK, DesignerUIPlugin.PLUGIN_ID, IStatus.OK, "", null); //$NON-NLS-1$
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jface.wizard.WizardPage#canFlipToNextPage()
     */
    @Override
    public boolean canFlipToNextPage() {
        boolean canFlipToNextPage = super.canFlipToNextPage();
        if (canFlipToNextPage) {
            ((NewElementWizardPage3) getNextPage()).setFileName(databaseName);
        }
        return canFlipToNextPage;
    }
}
